//
//  ABuyNowViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/9/27.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ABuyNowViewController.h"
#import "AGoodProductTableViewCell.h"
#import "AGoodProductModel.h"

#import "ANewArrivalViewController.h" //最新上架
#import "AGoodObjectViewController.h" //好物优选

@interface ABuyNowViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mytableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *mydateSource;
@property (nonatomic, strong) UIView *tabFootView;
@property (nonatomic, strong) NSMutableSet <NSString *>*saoYiSaoStrSet;
@property (nonatomic, strong) UIView *bgViewsec;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *modelArr;

@end

@implementation ABuyNowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    
    self.title = @"我要预订";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    //self.view.backgroundColor = [UIColor whiteColor];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    self.modelArr = [NSMutableArray arrayWithCapacity:1];
    
    [self showdetaile];
    
    [self requestData];
    // Do any additional setup after loading the view.
}

-(void)showdetaile {
    
    [self.view addSubview:self.scrollView];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.frame = CGRectMake(0, kNaviHeight, KWIDTH, 189);
    [self.scrollView addSubview:bgView];
    
    //左侧 配件
    UIButton *leftBtn = [[UIButton alloc] init];
    leftBtn.frame = CGRectMake(10, 0, (KWIDTH-30)/2, 189);
    [leftBtn setImage:imgname(@"lijiyuding_headerleft") forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:leftBtn];
    
    //右侧 整机
    UIButton *rightBtn = [[UIButton alloc] init];
    rightBtn.frame = CGRectMake(2 +(KWIDTH-14)/2 +10 , 0, (KWIDTH-30)/2, 189);
    [rightBtn setImage:imgname(@"lijiyuding_rightheader") forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:rightBtn];
    
    _mytableView = [[UITableView alloc] init];
    _mytableView.frame = CGRectMake(0, bgView.bottom, KWIDTH, KHEIGHT - kNaviHeight-189);
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    _mytableView.estimatedRowHeight = 90;
    _mytableView.rowHeight = UITableViewAutomaticDimension;
    _mytableView.tableFooterView = [UIView new];
    [_mytableView registerClass:[AGoodProductTableViewCell class] forCellReuseIdentifier:@"AGoodProductTableViewCell"];
    _mytableView.backgroundColor = [UIColor clearColor];
    adjustInset(_mytableView);
    [_mytableView reloadData];
    [_scrollView addSubview:_mytableView];
}

- (void)requestData {
    //我要预订 数据请求
    
    if (_page == 1) {
        [_modelArr removeAllObjects];
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    NSString *pp = [NSString stringWithFormat:@"%d",_page];
    param[@"pageNum"] = pp;
    param[@"type"] = @"1"; //我要预订 热火推荐 1
    
//    [NetWorkTool POST:getPartsList param:param success:^(id dic) {
//        NSLog(@"有没有数据呀 %@", dic);
    
//    self.modelArr = [AGoodProductModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
    
//        [self.mytableView reloadData];
//    } other:^(id dic) {
//        
//    } fail:^(NSError *error) {
//        
//    } needUser:NO];
}

- (void)leftBtnAction:(UIButton *)button {
    
    ANewArrivalViewController *goodVC = [[ANewArrivalViewController alloc] init];

    [self.navigationController pushViewController:goodVC animated:YES];
    NSLog(@"您点击了 右上按钮");
}

- (void)rightBtnAction:(UIButton *)button {
    
    AGoodObjectViewController *newVC = [[AGoodObjectViewController alloc] init];

    [self.navigationController pushViewController:newVC animated:YES];
    NSLog(@"您点击了 右下按钮");
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //return _modelArr.count;
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 345;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //定义Cell标识 每一个Cell对应一个自己的标识
    NSString *CellIdentitier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section, indexPath.row];
    
    //通过唯一标识创建Cell实例
    AGoodProductTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentitier];
    //判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,w而不会再次初始化)
    if (!mycell) {
        mycell = [[AGoodProductTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentitier];
    }
    self.mytableView.separatorStyle = UITableViewCellEditingStyleNone;//不显示分割线
    
//    AGoodProductModel *model = self.modelArr[indexPath.row];
//    [mycell.goodImg sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:nil]; //产品图片
//    mycell.proTitLab.text = model.parts_name; //描述信息
//    mycell.numTitLab.text = [NSString stringWithFormat:@"%@%@", model.cellCount,@"人预订"];
    
    return mycell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

/**
 弹出框的背景图
 */
-(void)shwoBgviewsec{
    self.bgViewsec = [[UIView alloc]init];
    self.bgViewsec.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
    self.bgViewsec.backgroundColor = RGBA(1, 1, 1, 0.5);
    UIView *whiteBGView = [[UIView alloc]init];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    whiteBGView.layer.masksToBounds = YES;
    whiteBGView.layer.cornerRadius = 6;
    [self.bgViewsec addSubview:whiteBGView];
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.offset(240);
        make.left.offset(22);
        make.right.offset(-22);
    }];
    
    UIImageView *upImage = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH/2-22-20, 20, 40, 40)];
    upImage.layer.masksToBounds = YES;
    upImage.layer.cornerRadius = 20;
    [whiteBGView addSubview:upImage];
    upImage.image = [UIImage imageNamed:@"组732"];
    
    UILabel *UpLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 72, KWIDTH-44, 45)];
    [whiteBGView addSubview:UpLable];
    UpLable.font = FontSize(16);
    UpLable.numberOfLines = 0;
    UpLable.textColor = [UIColor colorWithHexString:@"#333333"];
    UpLable.textAlignment =  NSTextAlignmentCenter;
    UpLable.text = @"配件信息已提交\n可在已处理页面查看配件详情";
    
    UILabel *DowLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 103, KWIDTH-44, 21)];
    [whiteBGView addSubview:DowLable];
    DowLable.font = FontSize(14);
    DowLable.textColor = [UIColor colorWithHexString:@"#333333"];
    DowLable.textAlignment =  NSTextAlignmentCenter;
    //    DowLable.text = @"明天继续努力";
    UIButton *iKnowBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [iKnowBut setTitle:@"我知道了" forState:(UIControlStateNormal)];
    [iKnowBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [iKnowBut setBackgroundColor:[UIColor colorWithHexString:@"#70BE68"]];
    iKnowBut.layer.masksToBounds = YES;
    iKnowBut.layer.cornerRadius = 4;
    [iKnowBut addTarget:self action:@selector(ikenow:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:iKnowBut];
    [iKnowBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(48);
        make.left.offset(28);
        make.right.offset(-28);
        make.bottom.offset(-39);
    }];
}

-(void)ikenow:(UIButton *)but{
    [_bgViewsec removeFromSuperview];
    
    //    if (self.mybleoc) {
    //        self.mybleoc(@"");
    //    }
    //    [self.navigationController popViewControllerAnimated:YES];
    
    //[self.navigationController popViewControllerAnimated:YES];
    self.navigationController.tabBarController.selectedIndex = 4;
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
        _scrollView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(KWIDTH, 100);
        
        adjustInset(_scrollView);
    }
    return _scrollView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
